<template>
  <div class="main">
    <el-button type="primary" icon="el-icon-plus" @click="openDialog(0)"
      >添加</el-button
    >
    <el-table :data="list" border style="width: 100%; margin-top: 10px">
      <el-table-column type="index" label="序号" width="100px" align="center">
      </el-table-column>
      <el-table-column prop="tmName" label="品牌名称" width="width">
      </el-table-column>
      <el-table-column prop="logoUrl" label="品牌Logo" width="width">
        <template slot-scope="{ row }">
          <img :src="row.logoUrl" alt="" style="width: 80px; height: 80px" />
        </template>
      </el-table-column>
      <el-table-column prop="id" label="操作" width="width">
        <template slot-scope="{ row }">
          <el-button
            type="warning"
            icon="el-icon-edit"
            @click="openDialog(row.id)"
            >修改</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete-solid"
            @click="delTrade(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="text-align: center"
      :current-page="1"
      :page-sizes="[3, 5, 7, 10]"
      :page-size="3"
      layout=" prev, pager, next, jumper, ->, sizes ,total "
      :total="total"
    >
    </el-pagination>

    <el-dialog title="添加品牌" :visible.sync="dialogFormVisible">
      <el-form :model="tradeform" :rules="rules" ref="ruleForm">
        <el-form-item
          label="品牌名称"
          label-width="120px"
          style="width: 80%"
          prop="tmName"
        >
          <el-input v-model="tradeform.tmName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="品牌Logo"
          label-width="120px"
          style="width: 300px"
          prop="logoUrl"
        >
          <el-upload
            class="avatar-uploader"
            action="/admin/product/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="tradeform.logoUrl"
              :src="tradeform.logoUrl"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload
        ></el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateTrade">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Trade",
  data() {
    return {
      page: 1,
      limit: 3,
      list: [],
      total: 0,
      dialogFormVisible: false,
      tradeName: "",
      tradeform: {
        logoUrl: "",
        tmName: "",
      },
      rules: {
        tmName: [
          { required: true, message: "请输入名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "change",
          },
        ],
        logoUrl: [{ required: true, message: "请放入图片" }],
      },
    };
  },
  mounted() {
    this.getTradesList();
  },
  methods: {
    async getTradesList() {
      let result = await this.$api.default.trade.getTradeList(
        this.page,
        this.limit
      );
      if (result.code == 200) {
        this.list = result.data.records;
        this.total = result.data.total;
      }
    },
    handleSizeChange(size) {
      this.limit = size;
      this.getTradesList();
    },
    handleCurrentChange(pager) {
      this.page = pager;
      this.getTradesList();
    },
    async openDialog(type = 0) {
      this.tradeform.id = null;
      this.tradeform.logoUrl = "";
      this.tradeform.tmName = "";
      if (type != 0) {
        this.tradeform.id = type;
        let result = await this.$api.default.trade.getTrade(type);
        if (result.code == 200) {
          this.tradeform.logoUrl = result.data.logoUrl;
          this.tradeform.tmName = result.data.tmName;
        }
      }
      this.dialogFormVisible = !this.dialogFormVisible;
    },
    handleAvatarSuccess(res, file) {
      this.tradeform.logoUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    addOrUpdateTrade() {
      this.$refs.ruleForm.validate(async (success) => {
        if (success) {
          let result = await this.$api.default.trade.addOrUpdateTrade(
            this.tradeform
          );
          if (result.code == 200) {
            this.$message({
              message: "操作成功!",
              type: "success",
            });
            this.dialogFormVisible = false;
            this.getTradesList();
          } else {
            this.$message.error("操作失败!");
            return;
          }
        } else {
          this.$message.error("请按需求完成填写!");
          return false;
        }
      });
    },
    async delTrade(id) {
      let result = await this.$api.default.trade.delTrade(id);
      if (result.code == "200") {
        this.$message({
          message: "删除成功!",
          type: "success",
        });
        this.getTradesList();
      } else {
        this.$message.error("删除失败!");
      }
    },
  },
};
</script>

<style scoped>
.main {
  padding: 20px;
}
.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
